package com.pug.service.order;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pug.bo.UserOrderBo;
import com.pug.dto.UserOrderDto;
import com.pug.mapper.order.UserOrderMapper;
import com.pug.pojo.KssUser;
import com.pug.pojo.UserOrder;
import com.pug.result.ex.KsdBusinessException;
import com.pug.service.BaseService;
import com.pug.threadlocal.UserThreadLocal;
import com.pug.utils.fn.asserts.Vsserts;
import com.pug.vo.UserOrderVo;
import com.pug.vo.UserVo;
import lombok.extern.slf4j.Slf4j;
import org.apache.catalina.User;
import org.springframework.amqp.AmqpException;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageDeliveryMode;
import org.springframework.amqp.core.MessagePostProcessor;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.support.converter.AbstractJavaTypeMapper;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * Author: lqm
 * create: 2022/4/3 20:15
 * Lover: lqx
 *
 * @see
 */
@Service
@Slf4j
public class UserOrderProducer{
    public static final String order_direct_queue="order_direct_queue";
    public static final String order_direct_exchange="order_direct_exchange";
    public static final String order_direct_key="order_direct_key";
    @Resource
    RabbitTemplate rabbitTemplate;
    public void pushOrder(UserOrderDto userOrderDto){
        try{
            //        rabbitTemplate.setMandatory();
            // 1: 设置对象转码 覆盖全局，更更改
            rabbitTemplate.setMessageConverter(new Jackson2JsonMessageConverter());
            rabbitTemplate.setExchange(order_direct_exchange);
            rabbitTemplate.setRoutingKey(order_direct_key);
            rabbitTemplate.convertAndSend(userOrderDto, new MessagePostProcessor() {
                @Override
                public Message postProcessMessage(Message message) throws AmqpException {
                    MessageProperties messageProperties = message.getMessageProperties();
                    messageProperties.setDeliveryMode(MessageDeliveryMode.PERSISTENT); //设置持久化策略
                    //设置头
                    messageProperties.setHeader(AbstractJavaTypeMapper.DEFAULT_CONTENT_CLASSID_FIELD_NAME,UserOrderDto.class);
                    //设置额外参数
                    messageProperties.setHeader("msgtype",1);
                    return message;
                }
            });
            // 5: 打印日志
            log.info("用户下单支付超时 - 发送用户下单死信队列的内容是：{}", userOrderDto);
            // 6：可靠生产问题
            // - 确认机制
            // - 错误处理
        }catch (Exception e){
            log.info("用户下单支付超时 - 发送用户下单死信队列出现异常，出现异常的内容是：{}", userOrderDto);
        }
    }



}
